package com.yami.shop.combo.multishop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Giveaway;
import com.yami.shop.combo.multishop.service.GiveawayService;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;


/**
 * 赠品
 *
 * @author LGH
 * @date 2021-11-08 13:29:16
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/giveaway" )
@Tag(name="赠品接口")
public class GiveawayController {

    private final GiveawayService giveawayService;

    @GetMapping("/page" )
    @Operation(summary =  "分页获取赠品信息", description = "分页获取赠品信息")
    public ServerResponseEntity<IPage<Giveaway>> getGiveawayPage(PageParam<Giveaway> page,@ParameterObject Giveaway giveaway) {
        giveaway.setShopId(SecurityUtils.getShopUser().getShopId());
        IPage<Giveaway> giveawayPage = giveawayService.pageByParam(page, giveaway);
        return ServerResponseEntity.success(giveawayPage);
    }

    @GetMapping("/info/{giveawayId}" )
    @Operation(summary =  "通过id查询赠品", description = "通过id查询赠品")
    public ServerResponseEntity<Giveaway> getById(@PathVariable("giveawayId") Long giveawayId) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        Giveaway giveaway = giveawayService.getInfoById(giveawayId);
        if (!Objects.equals(shopId, giveaway.getShopId())) {
            throw new YamiShopBindException("yami.giveaway.not.on.shop");
        }
        return ServerResponseEntity.success(giveaway);
    }

    @PostMapping
    @Operation(summary =  "新增赠品", description = "新增赠品")
    public ServerResponseEntity<Void> save(@RequestBody @Valid Giveaway giveaway) {
        giveaway.setShopId(SecurityUtils.getShopUser().getShopId());
        giveawayService.saveInfo(giveaway);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary =  "修改赠品", description = "修改赠品")
    public ServerResponseEntity<Void> updateById(@RequestBody @Valid Giveaway giveaway) {
        giveaway.setShopId(SecurityUtils.getShopUser().getShopId());
        giveawayService.updateInfo(giveaway);
        return ServerResponseEntity.success();
    }

    @PutMapping("/changeStatus")
    @Operation(summary =  "修改赠品状态", description = "修改赠品状态")
    public ServerResponseEntity<Void> changeStatus(@RequestParam("status") Integer status, @RequestParam("giveawayId") Long giveawayId) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        giveawayService.changeStatus(giveawayId, shopId, status);
        return ServerResponseEntity.success();
    }
}
